local juguan = fk.CreateSkill {
  name = "3s__juguan",
}

Fk:loadTranslationTable{
  ["3s__juguan"] = "拒关",
  [":3s__juguan"] = "当你需要对处于濒死状态的角色使用【桃】时，你可以重铸两张字数相同的牌，若名称也相同，其死亡。一名角色使用【杀】时，若你在此牌最短路径上，你可以重铸两张字数相同的牌，若名称也相同，取消此【杀】所有目标。",
  ["#juguan-recast"] = "你可以重铸两张字数相同的牌，若名称也相同，%src死亡",
  ["#juguan-slash"] = "你可以重铸两张字数相同的牌，若名称也相同，取消此%arg所有目标",
  ["#juguan_cancel"] = "%from 使用的 %arg 取消所有目标",
}

juguan:addEffect(fk.AskForPeaches, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(juguan.name) and target == player
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local dying = data.who
    if not dying.dead then
      local success, dat = room:askToUseActiveSkill(player, {
        skill_name = "juguan_active",
        prompt = "#juguan-recast:"..dying.id,
        cancelable = true,
        skip = true,
     })
      if success and dat then
        event:setCostData(self, {cards = dat.cards})
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = event:getCostData(self).cards
    room:recastCard(cards, player, juguan.name)
    if Fk:getCardById(cards[1]).trueName == Fk:getCardById(cards[2]).trueName then
      local deathData = {
        who = data.who,
        killer = data.damage and data.damage.from,
        damage = data.damage,
       }
      room:killPlayer(deathData)
    end
  end,
})

--获取短路径之间的角色
local function getShortRoute(player, target)
  if player == target then return {} end
  local nexts = {}
  local lasts = {}
  local p = player
  while true do
    p = p:getNextAlive(false)
    table.insert(nexts, p)
    if p == target then break end
  end
  p = player
  while true do
    p = p:getLastAlive(false)
    table.insert(lasts, p)
    if p == target then break end
  end
  if #nexts <= #lasts then
    return nexts
  else
    return lasts
  end
end

juguan:addEffect(fk.AfterCardTargetDeclared, {
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(juguan.name) and data.card.trueName == "slash" and data.from ~= player and #data:getAllTargets() > 0 then
      for _, t in ipairs(data:getAllTargets()) do
        local route = getShortRoute(data.from, t)
        if table.contains(route, player) then
          return true
        end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local success, dat = room:askToUseActiveSkill(player, {
        skill_name = "juguan_active",
        prompt = "#juguan-slash:::"..data.card:toLogString(),
        cancelable = true,
        skip = true,
     })
    if success and dat then
      event:setCostData(self, {cards = dat.cards})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = event:getCostData(self).cards
    room:recastCard(cards, player, juguan.name)
    if Fk:getCardById(cards[1]).trueName == Fk:getCardById(cards[2]).trueName then
      room:sendLog{ type = "#juguan_cancel", from = data.from.id, arg = data.card:toLogString(), toast = true }
      data:removeAllTargets()
    end
  end
})

return juguan